<?
include_once 'CoreModel.php';
Class ConnectSapModel extends CoreModel {
	private $table;
	public function __construct() {
		$this -> table = "user_display_tab";
		parent::__construct();
	}

	public function insertConnectData($result) {
		/* $result is result from sap */
		//	print_r($result);
		//exit();
		// default log message
		$data_log['log_function'] = "SO_USER_LIST_READ";
		$data_log['log_table'] = "user_display_tab";
		$data_log['log_record'] = 0;
		$data_log['log_type'] = "INSERT";
		$data_log['log_status'] = "COMPLETED";
		$data_log['log_by_name'] = "Batch Job";
		$data_log['log_page'] = "connectsap";

		$numRows = 0;
		// start begin trans
		$this -> db -> trans_begin();
		$this -> db -> query('delete from user_display_tab');
		$dataInsert = array();
		
		foreach ($result["USER_DISPLAY_TAB"] as $user) {
			/*$cus["CREDIT_LIMIT"] = convertValue($cus["CREDIT_LIMIT"]);
			$cus["CREDIT_USED"] = convertValue($cus["CREDIT_USED"]);
			$cus["CREDIT_LEFT"] = convertValue($cus["CREDIT_LEFT"]);
			$dataInsert[] = $user;*/
			$data = array();
			$data['SAPNAM'] = $user['SAPNAM'];
			$data['USRNO'] = $user['USRNO'];
			$dataInsert[] = $data;
		}

		// insert into USER_DISPLAY_TAB;
		$this -> db -> insert_batch($this -> table, $dataInsert);

		if ($this -> db -> trans_status() === FALSE) {
			// if error on tranaction
			$this -> db -> trans_rollback();
				
			// insert log  of table customer data
			
			$this->insertLogFailed('USER_DISPLAY_TAB', 'connectsap');
			//$this->insertLogFailed('SALES_DATA');
			
			return array("flag" => false, "message" => "error on transfer");
		} else {
			// when complete
			$this -> db -> trans_commit();
			$this->insertLogComplete('USER_DISPLAY_TAB', count($dataInsert), 'connectsap');
			//$this->insertLogComplete('SALES_DATA', count($result["SALES_DATA"]));		
			//return array("flag" => true, "customer_row" => count($dataInsert), "sale_row" => count($result["SALES_DATA"]));	
			//return ค่าไป controller
			return array("flag" => true, "connect_row" => count($dataInsert));
		}
	}

	/*public function defaultSearch($search) {

		if (empty($search)) {
			return;
		}

		if (isset($search['KUNNR']) && $search['KUNNR'] != "") {
			$this -> db -> like("KUNNR", $search['KUNNR']);
		}
		if (isset($search['NAME']) && $search['NAME'] != "") {
			$this -> db -> like("NAME", $search['NAME']);
		}
		if (isset($search['ADDRESS']) && $search['ADDRESS'] != "") {
			$this -> db -> like("ADDRESS", $search['ADDRESS']);
		}
	}*/

}
?>